package com.kexun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kexun.entity.DO.Shop;
import com.kexun.mapper.ShopMapper;
import com.kexun.service.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopServiceImpl extends ServiceImpl<ShopMapper, Shop> implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public List<Shop> findShopByDeptId(int depId) {
        return shopMapper.findShopByDeptId(depId);
    }

    @Override
    public IPage<Shop> findShopList(String shopName, int page, int limit) {
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(shopName)) {
            shopQueryWrapper.eq("shop_name", shopName);
        }
        IPage<Shop> shopIPage = shopMapper.selectPage(new Page<Shop>(page, limit), shopQueryWrapper);
        return shopIPage;
    }

    @Override
    public void updShowState(boolean state, int id) {
        Shop shop = shopMapper.selectById(id);
        shopMapper.updateById(shop);
    }

    @Override
    public void addShop(Shop shop) {
        shopMapper.insert(shop);
    }

    @Override
    public void deleteById(int id) {
        shopMapper.deleteById(id);
    }

    @Override
    public List<Shop> findAllShopList() {
        return shopMapper.selectList(null);
    }

    @Override
    public List<Shop> findHasShopList(int depId) {
        List<Shop> allShopList = findAllShopList();
        List<Shop> shopByDeptId = findShopByDeptId(depId);

        for (Shop shop : allShopList) {
            for (Shop shop1 : shopByDeptId) {
                if (shop.getId() == shop1.getId()) {
                    shop.setHas(true);
                }
            }
        }
        return allShopList;
    }

    @Override
    public String findByShopVal(Integer shop) {
        QueryWrapper<Shop> shopQueryWrapper = new QueryWrapper<>();
        shopQueryWrapper.eq("shop_value", shop);
        shopQueryWrapper.last(" limit 1");
        Shop shop1 = shopMapper.selectOne(shopQueryWrapper);
        return shop1.getShopName();
    }
}
